//
// Created by tonystark on 16/4/14.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "JPushHander.h"
#import "JPUSHService.h"
#import "SystemManager.h"


@implementation JPushHander {

}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (void)addObserver {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kJPFNetworkDidSetupNotification object:nil];
    [defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kJPFNetworkDidCloseNotification object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidRegister:)
                          name:kJPFNetworkDidRegisterNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidLogin:)
                          name:kJPFNetworkDidLoginNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidReceiveMessage:)
                          name:kJPFNetworkDidReceiveMessageNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(serviceError:)
                          name:kJPFServiceErrorNotification
                        object:nil];
}

+ (void)networkDidSetup:(NSNotification *)notification {
    DEBUG_LOG(@"%@", @"已连接");
}

+ (void)networkDidClose:(NSNotification *)notification {
    DEBUG_LOG(@"%@", @"未连接");
}

/**
 * 已注册
 */
+ (void)networkDidRegister:(NSNotification *)notification {
    DEBUG_LOG(@"%@", [notification userInfo]);
    DEBUG_LOG(@"%@", @"已注册");
}

/**
 * 已登录
 */
+ (void)networkDidLogin:(NSNotification *)notification {
    DEBUG_LOG(@"%@", @"已登录");
    if ([JPUSHService registrationID]) {
        DEBUG_LOG(@"get RegistrationID  %@", [JPUSHService registrationID]);
    }
//    SystemSettingManager *manager = [SystemSettingManager sharedInstance];
//    [JPUSHService setTags:[NSSet setWithObject:@"chenfei"] alias:manager.guid fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
//        DEBUG_LOG(@"alias callback %d,%@", iResCode,iAlias);
//    }];
//    [self setTag];
}

+ (void)setTag {
    SystemManager *manager = [SystemManager sharedInstance];
    NSString *pushID = [NSString stringWithFormat:@"tag_user_%@",manager.clientId];
    NSString *aliasID = [NSString stringWithFormat:@"alias_department_%@",manager.departmentId];
    if (pushID && pushID.length > 0) {
        [JPUSHService setTags:[NSSet setWithObject:pushID] alias:aliasID callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
    } else {
        DEBUG_LOG(@"%@", @"user pushID 不存在");
    }
}

+ (void)tagsAliasCallback:(int)iResCode tags:(NSSet *)tags alias:(NSString *)alias {
    DEBUG_LOG(@"%@",[NSString stringWithFormat:@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags, alias]);
}


+ (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary * userInfo = [notification userInfo];
    NSString * title = [userInfo valueForKey:@"title"];
    NSString * content = [userInfo valueForKey:@"content"];
    NSDictionary * extra = [userInfo valueForKey:@"extras"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    NSString * currentContent = [NSString
            stringWithFormat:
                    @"收到自定义消息:%@\ntitle:%@\ncontent:%@\nextra:%@\n",
                    [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                   dateStyle:NSDateFormatterNoStyle
                                                   timeStyle:NSDateFormatterMediumStyle],
                    title, content, [self logDic:extra]];
    DEBUG_LOG(@"%@", currentContent);
}

/**
 *  错误提示
 */
+ (void)serviceError:(NSNotification *)notification {
    DEBUG_LOG(@"%@", notification);
}

+ (NSString *)logDic:(NSDictionary *)dic {
    if (![dic count]) {
        return nil;
    }
    NSString * tempStr1 =
            [[dic description] stringByReplacingOccurrencesOfString:@"\\u"
                                                         withString:@"\\U"];
    NSString * tempStr2 =
            [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString * tempStr3 =
            [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString * str =
            [NSPropertyListSerialization propertyListFromData:tempData
                                             mutabilityOption:NSPropertyListImmutable
                                                       format:NULL
                                             errorDescription:NULL];
    return str;
}

@end